package org.ykh.gmall.product;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.ykh.gmall.common.service.constant.RedisConst;

/**
 * @author ykh
 */
@SpringBootApplication(scanBasePackages = {
        "org.ykh.gmall.product",//product
        "org.ykh.gmall.common",//gmall_common
        "org.ykh.common",//common
        "org.ykh.service",//service
        "org.ykh.oos"//oos
})
//oos,redission相关
@ConfigurationPropertiesScan({"org.ykh.oos.config.property","org.ykh.service.property"})
@EnableDiscoveryClient//启用注册中中心
//@EnableFeignClients//启用feign
public class GmallProductApplication implements CommandLineRunner, ApplicationContextAware {
    public static void main(String[] args) {
        System.out.println("-------main--------");
        SpringApplication.run(GmallProductApplication.class,args);
    }
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) {
        System.out.println("-------mainrun--------");
        final Environment environment = applicationContext.getBean(Environment.class);
        boolean isTest = false;
        boolean isRedis = false;
        for (String activeProfile : environment.getActiveProfiles()) {
            switch (activeProfile){
                case "test":
                    isTest = true;
                    break;
                case "redis":
                    isRedis = true;
                    break;
                default:
            }


        }
        if (!isTest || isRedis){
            RedissonClient redissonClient = applicationContext.getBean(RedissonClient.class);
            //        //获取布隆过滤器
            RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
            //初始化
            bloomFilter.tryInit(100000L,0.01);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
